//
//  SYRegistViewController.m
//  BSHOUYIFANG
//
//  Created by lanou3g on 15/8/18.
//  Copyright (c) 2015年 CYB. All rights reserved.
//

#import "SYRegistViewController.h"
#import "Header.h"
#import "SYProtocolViewController.h"
#import "SYLoginViewController.h"
#import "SYRegisterView.h"
#import "SYMyFmdb.h"
#import "SYFileHandle.h"
#import "SYIdentifyingCodeViewController.h"
#import <SMS_SDK/SMSSDK.h>
@interface SYRegistViewController ()

@property (nonatomic,strong)SYRegisterView *registerView;
@property (nonatomic,strong)UILabel *promptlabel;
@end

@implementation SYRegistViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title =@"使用手机号注册";
    _registerView = [[SYRegisterView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    [self.view addSubview:_registerView];
    
    //注册页面的几个点击按钮响应的方法
    [_registerView.nextButton addTarget:self action:@selector(nextButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    [_registerView.protocolButton addTarget:self action:@selector(protocolButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    [_registerView.yetButton addTarget:self action:@selector(yetButtonAction:) forControlEvents:UIControlEventTouchUpInside];

   
}
// 在注册界面 如果没有任何输入 跳出提示框
- (void)nextButtonAction:(UIButton *)sender{
    [_registerView endEditing:YES];
    SYUser *user =[[SYMyFmdb shareInstance] getUserInfophoneNumber:_registerView.phoneNumberTF.text];
    _promptlabel = [[UILabel alloc] initWithFrame:CGRectMake(SCREEN_WIDTH/2-50, SCREEN_HEIGHT/2-10, SCREEN_WIDTH/3+10, 40)];
    _promptlabel.font = [UIFont systemFontOfSize:13];
    _promptlabel.textColor = [UIColor redColor];
    _promptlabel.textAlignment = NSTextAlignmentCenter;
    _promptlabel.backgroundColor = [UIColor blackColor];
    if ([[_registerView phoneNumberText] isEqualToString:@""]) {
        
        [self.view addSubview:_promptlabel];
        _promptlabel.text =@"请填写手机号码!";
        [_promptlabel performSelector:@selector(removeFromSuperview) withObject:nil afterDelay:2];//延迟三秒触发事件；
        return;
    }else if([[_registerView phoneNumberText] length] !=11 || [[_registerView phoneNumberText]characterAtIndex:0] !='1')
    {
        _promptlabel.frame = CGRectMake(SCREEN_WIDTH/2-60, SCREEN_HEIGHT/2-10, SCREEN_WIDTH/3+20, 40);
        _promptlabel.text =@"手机号格式不对，请重新填写!";
        _promptlabel.numberOfLines =2;
        [self.view addSubview:_promptlabel];
        [_promptlabel performSelector:@selector(removeFromSuperview) withObject:nil afterDelay:2];
        return;
    }else if([user.phoneNumber isEqualToString:_registerView.phoneNumberTF.text])
    {
        _promptlabel.frame = CGRectMake(SCREEN_WIDTH/2-60, SCREEN_HEIGHT/2-10, SCREEN_WIDTH/3+20, 40);
        _promptlabel.text =@"您已经注册,请不要重复注册!!!";
         [self.view addSubview:_promptlabel];
         _promptlabel.numberOfLines =2;
        [_promptlabel performSelector:@selector(removeFromSuperview) withObject:nil afterDelay:2];
        return;
    }
//    NSLog(@"%@",_registerView.phoneNumberTF.text);
//    [SMSSDK getVerificationCodeBySMSWithPhone:_registerView.phoneNumberTF.text zone:@"86" result:^(SMS_SDKError *error) {
//    }];
    [SMSSDK getVerificationCodeByMethod:0 phoneNumber:_registerView.phoneNumberTF.text zone:@"86" customIdentifier:nil result:^(NSError *error) {
        
    }];
  //保存注册的用户信息
    [[SYFileHandle shareInstance] setPhoneNumber:[_registerView phoneNumberText]];
    
    SYIdentifyingCodeViewController *IdCodeVC =[[SYIdentifyingCodeViewController alloc] init];
    IdCodeVC.str=_registerView.phoneNumberText;
    [self.navigationController pushViewController:IdCodeVC animated:YES];
}

- (void)protocolButtonAction:(UIButton *)sender{
    SYProtocolViewController *protocolVC = [[SYProtocolViewController alloc] init];
    [self.navigationController pushViewController:protocolVC animated:YES];
}

- (void)yetButtonAction:(UIButton *)sender{
//    SYLoginViewController *longinVC =[[SYLoginViewController alloc]init];
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
